/**
 * 
 */
package org.xelnaga.defiler.web.controller.folder;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xelnaga.defiler.domain.FolderModel;
import org.xelnaga.defiler.domain.SiteModel;
import org.xelnaga.defiler.service.FolderService;
import org.xelnaga.defiler.service.SiteService;
/**
 * @author qijb
 *
 */
@Controller("generatedFolderRestController")
@RequestMapping(value = "domain/rest/folder")
public class FolderRestController {
    protected FolderFormService folderFormService;
    @Autowired
    private FolderService folderService;
    @Autowired
    private SiteService siteService;
    
    @Autowired
    public void setFolderFormService(FolderFormService folderFormService) {
        this. folderFormService =  folderFormService;
    }

    protected FolderService folderManager;
    
    @Autowired
	public void setFolderManager(FolderService folderManager) {
		this.folderManager = folderManager;
	}

	/**
	 * 
	 */
	public FolderRestController() {
		// TODO Auto-generated constructor stub
	}
	    
    @RequestMapping(value = "/children/{folderId}")
    @ResponseBody
    public Object children(@PathVariable String folderId, HttpServletRequest request, HttpServletResponse response) {
    	List<FolderMessage> list = new ArrayList<FolderMessage>();
    	if(StringUtils.equalsIgnoreCase(folderId, "root")) {
    		List<SiteModel> hostList = folderFormService.populateHostList();
    		for(SiteModel host : hostList) {
    			FolderMessage hostMessage = new FolderMessage(host);
    			list.add(hostMessage);
    			List<FolderModel> childs = null;//this.folderFormService.populatFolderList(host.getInode());
    			if(null == this.siteService.getByPrimaryKey(host.getInode())) {
    				childs = this.folderService.getListByParentInode(host.getInode(), false);
    			} else {//This is host inode
    				childs = this.folderService.getListBySiteInode(host.getInode());
    			}
    			for(FolderModel folder : childs) {
    				FolderMessage folderMessage = new FolderMessage(folder);
    				hostMessage.addChild(folderMessage);
    			}
    		}
    		return list;
    	} else if(StringUtils.isNumeric(folderId)) {//This is folder
    		FolderMessage current = new FolderMessage(folderManager.getByPrimaryKey(new Long(folderId)));
    		List<FolderModel> folders = folderFormService.populatFolderList(new Long(folderId));
    		for(FolderModel folder : folders) {
    			FolderMessage message = new FolderMessage(folder);
    			current.addChild(message);    				
    		}
    		if(null == current.getChildren()) {
    			current.setChildren(new ArrayList<FolderMessage>());
    		}
    		return current;
    	} else {
    		return new FolderMessage();
    	}
    }

}
